package com.ssm.sfzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.sfzy.entity.Course;
import com.ssm.sfzy.entity.CourseMark;
import com.ssm.sfzy.entity.User;
import com.ssm.sfzy.mapper.CourseMapper;
import com.ssm.sfzy.mapper.CourseMarkMapper;
import com.ssm.sfzy.mapper.UserMapper;
import com.ssm.sfzy.service.CourseMarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.ListResourceBundle;
import java.util.Map;

/**
 * @author jitwxs
 * @date 2024年04月01日 9:36
 */
@Service
public class CourseMarkServiceImpl implements CourseMarkService {

    @Autowired
    private CourseMarkMapper courseMarkMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public void add(CourseMark entity) {
        courseMarkMapper.insert(entity);
    }

    @Override
    public void edit(CourseMark entity) {
        courseMarkMapper.update(entity);
    }

    @Override
    public void remove(Integer id) {
        courseMarkMapper.delete(id);
    }

    @Override
    public void removeBatch(Integer[] ids) {
        courseMarkMapper.deleteBatch(ids);
    }

    @Override
    public List<CourseMark> find(CourseMark entity) {
        return courseMarkMapper.select(entity);
    }

    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, CourseMark entity) {
        PageHelper.startPage(pageNum,pageSize);
        List<CourseMark> list = courseMarkMapper.select(entity);
        for (CourseMark courseMark : list){
            Integer courseId = courseMark.getCourseId();
            Integer userId = courseMark.getUserId();
            String username = userMapper.selectById(userId).getUsername();
            String courseName = courseMapper.selectById(courseId).getCourseName();
            courseMark.setCourseName(courseName);
            courseMark.setUsername(username);
        }

        PageInfo<CourseMark> pageInfo = new PageInfo<>(list);
        Map<String,Object> map = new HashMap<>();
        map.put("list",pageInfo.getList());
        map.put("total",pageInfo.getTotal());
        return map;
    }

    @Override
    public CourseMark findById(Integer id) {
        return courseMarkMapper.selectById(id);
    }

    @Override
    public List<CourseMark> getMarkAndUser(Integer id) {
        List<CourseMark> courseMarks = courseMarkMapper.getMarkAndUser(id);

        for (CourseMark courseMark : courseMarks){
            Integer userId = courseMark.getUserId();
            User user = userMapper.selectById(userId);
            Integer courseId = courseMark.getCourseId();
            Course course = courseMapper.selectById(courseId);

            courseMark.setUsername(user.getUsername());
            courseMark.setCourseName(course.getCourseName());
        }
        return courseMarks;
    }
}
